package com.xiaoxiong.text;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


public class WeekList {
	private ArrayList<String> itemList;
	private Context context;
	private PopupWindow popupWindow;
	private ListView listView;
	private Handler weekHandler;
	private int thisWeek;

	DisplayMetrics dm = new DisplayMetrics();
	/** 屏幕宽度 **/
	private int screenWidth;
	Activity activity;

	public WeekList(Context context, Handler weekHandler) {
		this.context = context;
		this.weekHandler = weekHandler;
		this.activity = (Activity) context;

		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;

		itemList = new ArrayList<String>();

		View view = LayoutInflater.from(context).inflate(
				R.layout.week_pull_list, null);

		// 设置 listview
		listView = (ListView) view.findViewById(R.id.listView);
		listView.setAdapter(new PopAdapter());
		// listView.setOnItemClickListener(this);

		popupWindow = new PopupWindow(view, context.getResources()
				.getDimensionPixelSize(R.dimen.popmenu_width),
				LayoutParams.WRAP_CONTENT);

		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	// 批量添加菜单项
	public void addItems(String[] items) {
		for (String s : items)
			itemList.add(s);
	}

	// 下拉式 弹出 pop菜单 parent 右下角
	public void showAsDropDown(View parent, int thisWeek) {

		this.thisWeek = thisWeek;

		//pop菜单显示在中间
		int xoff = (screenWidth / 2 - parent.getWidth()) / 2;

		popupWindow.showAsDropDown(parent,
				-xoff,
				// 保证尺寸是根据屏幕像素密度来的
				context.getResources().getDimensionPixelSize(
						R.dimen.popmenu_yoff));

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 刷新状态
		popupWindow.update();
		listView.setSelection(thisWeek - 1);
	}

	// 隐藏菜单
	public void dismiss() {
		popupWindow.dismiss();
	}

	// 适配器
	private class PopAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return itemList.size();
		}

		@Override
		public Object getItem(int position) {
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = View.inflate(context, R.layout.week_list_item, null);

			TextView tv_id = (TextView) view.findViewById(R.id.textView);

			String wee = itemList.get(position);
			if ((position + 1) == thisWeek) {
				wee += "(本周)";
			}
			tv_id.setText(wee);

			tv_id.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Message msg = new Message();
					Bundle bundle = new Bundle();
					String str = (String) ((TextView) v).getText();
					StringBuilder strb = new StringBuilder(str);
					if (strb.indexOf("(本周)") > 1) {
						strb.delete(strb.indexOf("(本周)"), strb.length());
					}
					str = strb.toString();
//					Log.d("week", str);
					bundle.putString("position", str);
					msg.setData(bundle);

					weekHandler.sendMessage(msg);
				}
			});

			return view;
		}
	}
}
